<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .box {
        width: 100px;
        height: 2000px;
        background: blue;
      }

      .search {
        width: 300px;
        height: 50px;
        background: red;
        position: absolute;
        left: 50%;
        transform: translateX(-50%);
        top: 100px;
      }
    </style>
  </head>

  <body>
    <div class="box"></div>
    <div class="search"></div>
    <script>
      var searchEle = document.querySelector(".search");

      window.onscroll = function () {
        var num = document.documentElement.scrollTop;
        console.log(num);
        if (num >= 100) {
          searchEle.style.position = "fixed";
          searchEle.style.top = "0px";
        } else {
          searchEle.style.position = "absolute";
          searchEle.style.top = "100px";
        }
      };
    </script>
  </body>
</html>
